<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/9/6
 * Time: 10:42
 */
namespace app\admin\controller;
use think\Db;
class Answer extends Base {
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $map =array();
            $page = input('page') ? input('page'):1;
            $limit = input("limit")?input("limit"):10;// 获取总条数;
            if(isset($keyword)&&$keyword!="")
            {
                $map['uuu.user_phone'] = ['like',"%" . $keyword . "%"];
            }
            if(isset($subject)&&$subject!="")
            {
                $map['us.subject_id'] = $subject;
            }
            if(isset($sex)&&$sex!="")
            {
                $map['course_status'] = $sex;
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['a.course_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['a.course_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['a.course_time'] = ['between time',[$start,$end]];
            }
            $count = Db::table('up_course')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.course_user_id','left')
                ->join('up_subject us','us.subject_id = a.course_subject_id','left')
                ->where($map)
                ->count();
            $list = Db::table('up_course')->alias('a')
                ->join('up_u_user uuu','uuu.user_id = a.course_user_id','left')
                ->join('up_subject us','us.subject_id = a.course_subject_id','left')
                ->where($map)
                ->field('a.*,us.subject_name,uuu.user_username,uuu.user_phone')
                ->order('course_time desc')
                ->page($page, $limit)
                ->select();//计算总页面
            foreach ($list as $key=>&$vo){
                if($vo['course_type']==1){//指定老师
                    $info = Db::table('up_u_user')->where('user_id',$vo['course_teacher_id'])->find();
                    $vo['username'] = $info['user_username'];
                    $vo['user_phone'] = $info['user_phone'];
                }else{
                    $vo['username'] = '';
                    $vo['user_phone'] = '';
                }
            }
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$list]);
        }
        $subject = Db::table('up_subject')->order('subject_sort asc')->select();
        $this->assign('subject',$subject);
        return $this->fetch();
    }

    public function detail(){
        $map['course_id'] = input('param.id');
        $list = Db::table('up_course')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.course_user_id','left')
            ->join('up_subject us','us.subject_id = a.course_subject_id','left')
            ->where($map)
            ->field('a.*,us.subject_name,uuu.user_username,uuu.user_phone')
            ->find();//计算总页面
        if($list['course_type']==1){//指定老师
            $info = Db::table('up_u_user')->where('user_id',$list['course_teacher_id'])->find();
            $list['username'] = $info['user_username'];
            $list['user_phone'] = $info['user_phone'];
        }else{
            $list['username'] = '';
            $list['user_phone'] = '';
        }
        if($list['course_type']==1){
            $list['course_type_name'] = '指定老师';
        }else{
            $list['course_type_name'] = '公开';
        }
        if($list['course_status']!=1){
            $info1  = Db::table('up_course_reply')
                ->where('reply_course_id',$list['course_id'])->find();
            $info['username'] = Db::table('up_u_user')->where('user_id',$info1['reply_user_id'])->value('user_username');
            $info['img'] = explode(',',$info1['reply_img']);
        }
        $list['info'] = $info1;
        $list['img'] = explode(',',$list['couse_img']);
        $this->assign('res',$list);
        return $this->fetch();
    }
}